﻿using EnvDTE;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.TemplateWizard;
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace SPTotalAutomationToolkit.SP2013IntranetProjectTemplate.Wizard
{
    internal class ProjectWizardChild : IWizard
    {
        private ProjectSigningManager signingManager;
        private DTE dteObject;

        public ProjectWizardChild()
        {
            signingManager = new ProjectSigningManager();
        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            try
            {
                foreach (var entry in ProjectWizard.GlobalDictionary)
                {
                    replacementsDictionary.Add(entry.Key, entry.Value);
                }

                // Generate the signing key
                signingManager.GenerateKeyFile();
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.ToString());
            }
        }

        public void ProjectFinishedGenerating(Project project)
        {
            // If this is one of the SharePoint projects, set a dependency on the Deployment & Utilities projects and then set the SharePoint specific project settings
            if (!project.FullName.EndsWith("Deployment.csproj") && !project.FullName.EndsWith("Utilities.csproj"))
            {
                var solution = project.DTE.Solution;

                // Loop through the projects in the solution to find the deployment project
                foreach (Project prj in solution.Projects)
                {
                    if (prj.FullName.EndsWith("Deployment.csproj") || prj.FullName.EndsWith("Utilities.csproj"))
                    {
                        // Set a build dependency on the Deployment & Utilities projects in the solution
                        BuildDependency dependency = project.DTE.Solution.SolutionBuild.BuildDependencies.Item(project.UniqueName);
                        if (dependency != null)
                            dependency.AddProject(prj.UniqueName);
                    }
                }

                // Populate the SiteUrl and IsSandboxedSolution properties in the new project
                dteObject = project.DTE;
                var serviceProvider = new ServiceProvider(dteObject as IOleServiceProvider);
                var projectService = serviceProvider.GetService(typeof(ISharePointProjectService)) as ISharePointProjectService;
                
                ISharePointProject sharePointProject = projectService.Convert<Project, ISharePointProject>(project);
                sharePointProject.SiteUrl = new Uri(ProjectWizard.SiteUrlForDebugging, UriKind.Absolute);
                sharePointProject.IsSandboxedSolution = ProjectWizard.IsSandboxed;
                sharePointProject.AutoRetract = false;
            }

            // Add a new key.snk file to the project.
            signingManager.AddKeyFile(project);
        }

        // Always return true; this IWizard implementation throws a WizardCancelledException
        // that is handled by Visual Studio if the user cancels the wizard.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        // The following IWizard methods are not used in this example.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public void RunFinished()
        {
        }
    }
}